#define _CRT_SECURE_NO_WARNINGS 1
#include "stack.h"
void stackInit(ST* head)
{
	head->arr = NULL;
	head->top = 0;
	head->capacity = 0;
}

bool isEmpty(ST* head)
{
	if (head->top==0)
	{
		return true;
	}
	return false;
}
void stackPush(ST* head,STDataType data)
{
	assert(head);
	if (head->capacity == head->top)
	{
		int newCapacity = head->capacity==0 ? 4 : head->capacity * 2;
		STDataType* newArr =(STDataType*)realloc(head->arr, newCapacity * sizeof(STDataType));
		if (newArr == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		head->arr = newArr;
		head->capacity = newCapacity;
	}
	head->arr[head->top] = data;
	head->top++;
}

void stackPop(ST* head)
{
	assert(head);
	assert(head->top > 0);
	head->top--;
}

STDataType stackTop(ST* head)
{
	assert(head);
	assert(head->top > 0);
	return head->arr[(head->top)-1];
}

int stackSize(ST * head)
{
	assert(head);
	assert(head->arr);
	return head->top;
}

void stackDestroy(ST* head)
{
	assert(head);
	head->capacity = head->top = 0;
	free(head->arr);
	head->arr = NULL;
}